from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.api import mail
from domain import userprofile
from repository import userprofilerepository
from task import basetask

class UserProfileTask(basetask.BaseTask):
    def getAll(self):
        repository = userprofilerepository.UserProfileRepository()
        profiles = repository.getAll()
        self.logmessage("UserProfileTask retrieved all profiles, count = " + str(profiles.count()))
        return profiles

    def getSubscribers(self):
        repository = userprofilerepository.UserProfileRepository()
        profiles = repository.getSubscribers()
        self.logmessage("UserProfileTask retrieved all subscribers, count = " + str(profiles.count()))
        return profiles

    def sendSubscriberMessage(self, title, content):
        self.logmessage(title + ' : ' + content)
        profiles = self.getSubscribers()
        message = mail.EmailMessage()
        message.subject = title
        message.body = content
        message.sender = self.settings.adminmailbox
        for profile in profiles:
            message.to = profile.googleuser.email()
            message.send()
        self.logmessage("UserProfileTask delivered message titled " + str(title) + " to a total count of: " + str(profiles.count()))

    def getAllByRange(self, start, howmany):
        repository = userprofilerepository.UserProfileRepository()
        profiles = repository.getAllByRange(start, howmany)
        self.logmessage("UserProfileTask retrieved all profiles by range, start = " + str(start) + ", count = " + str(howmany))
        return profiles

    def getByUser(self, userobject):
        if (isinstance(userobject, users.User)):
            repository = userprofilerepository.UserProfileRepository()
            profile = repository.getByUser(userobject)
            if profile:
                if profile.enabled:
                    self.logmessage("UserProfileTask retrieved profile: " + str(profile.googleuser))
                    return profile
                else:
                    return None
            else:
                return None
        else:
            return None

    def hasProfile(self, userobject):
        if (isinstance(userobject, users.User)):
            repository = userprofilerepository.UserProfileRepository()
            profile = repository.getByUser(userobject)
            if profile:
                return True
            else:
                return False
        else:
            return False


    def save(self, newprofile):
        if (isinstance(newprofile, userprofile.UserProfile)):
            repository = userprofilerepository.UserProfileRepository()
            if (repository.save(newprofile)):
                self.logmessage("UserProfileTask saved user profile: " + str(newprofile))
                return True
            else:
                self.logmessage("UserProfileTask could not save user profile: " + str(newprofile))
                return False
        else:
            return False

    def disable(self, oldprofile):
        if (isinstance(oldprofile, userprofile.UserProfile)):
            oldprofile.enabled = False
            self.save(oldprofile)
            self.logmessage("User profile disabled: " + str(oldprofile.googleuser))

    def enable(self, oldprofile):
        if (isinstance(oldprofile, userprofile.UserProfile)):
            oldprofile.enabled = True
            self.save(oldprofile)
            self.logmessage("User profile enabled: " + str(oldprofile.googleuser))

    def isNicknameInUse(self, nickname):
        repository = userprofilerepository.UserProfileRepository()
        matches = repository.getByNickname(nickname)
        if (matches):
            return True
        else:
            return False

    def getByKey(self, profilekey):
        thekey = db.Key(profilekey)
        repository = userprofilerepository.UserProfileRepository()
        return repository.getByKey(thekey)   